from Common.handle_log import do_log
import re
from pytest_check import check

# 顺序检索
events_sequential_list = [{"event_name": "跳转进度条到 00:00:45",
                                   "event_pattern": r'onGetCurrentTimeStamp seek : 00:00:\d{2} -> 00:00:\d{2}'},
                                  {"event_name": "空调52秒预下发",
                                   "event_pattern": r'onConditioner: ts = 00:00:53, density = 3, mode = 0, direction = 0'},
                                  {"event_name": "空调54秒正式下发",
                                   "event_pattern": r'onConditioner: ts = 00:00:54, density = 0, mode = 0, direction = 2'},
                                  {"event_name": "开始自然播放10s",
                                   "event_pattern": r'GetCurrentTimeStamp timestamp : \d+ - (\d{2}:\d{2}:\d{2}), interval : \d+'},
                                  {"event_name": "空调温度恢复", "event_pattern": r'recoverTemperature, mSwitchStateRow1 : 0'},
                                  {"event_name": "空调风量恢复","event_pattern": r'recoverWindDensity , mSwitchStateRow1 : 0'},
                                  {"event_name": "空调风向恢复","event_pattern": r'recoverWindDirection, mSwitchStateRow1 : 0'},
                                {"event_name": "空调温度恢复", "event_pattern": r'recoverTemperature, mSwitchStateRow1 : 0'},

                                  ]
def sequential_events_log(events_list: list, log_path,necessary_events=7,model=1):
    if necessary_events>len(events_list):
        necessary_events=len(events_list)
    if model==1 and len(events_list)==necessary_events:
        do_log.info(f"模式1:开始读取日志文件，必须顺序匹配事件{len(events_list)}个")
    if model==1 and len(events_list)>necessary_events:
        do_log.info(f"模式1:开始读取日志文件，必须顺序匹配必要事件{necessary_events}个,非必要事件{len(events_list)-necessary_events}个中必须匹配命中一个")
    if model==2:
        do_log.info(f"模式2:开始读取日志文件，必须顺序匹配必要事件{necessary_events}个,非必要顺序匹配事件{len(events_list)-necessary_events}个必须全部命中")
    with open(log_path) as file:
        log_lines = file.readlines()
    matched_events = []
    current_event_index = 0
    check_number=0
    line_index_must=0
    line_index=0
    log_open=True

    # 打印当前事件名

    while True :
        line=log_lines[line_index]
        if check_number < len(events_list):
            event_name = events_list[check_number]['event_name']
            event_pattern = events_list[check_number]['event_pattern']
            if log_open:
                do_log.info(
                "第{}次检测,检测事件名字为为{},检测关键词为:{}".format(check_number + 1, event_name, event_pattern))
                log_open=False
        if current_event_index >= len(events_list):
            break
        match = re.search(event_pattern, line)
        if match:
            matched_events.append((current_event_index, line.strip(), match.groups()))
            current_event_index += 1
            check_number += 1
            log_open=True
            do_log.info("第{}次打印检索到的日志行为{}".format(check_number,line.strip()))
            if current_event_index == necessary_events:
                do_log.info(f"必要事件验证成功。期望的必要事件数量为{necessary_events},匹配到的事件数量为{current_event_index}")
                line_index_must=line_index
            if (current_event_index==necessary_events+1 or current_event_index==len(events_list)) and model==1:
                do_log.info(
                    f"整个事件验证成功。匹配成功事件为{current_event_index},必要事件数量为{necessary_events},可选事件为:{current_event_index-necessary_events}")
                break
            if current_event_index==len(events_list):
                do_log.info(
                    f"整个事件验证成功。匹配成功事件为{current_event_index},必要事件数量为{necessary_events},必要非顺序事件为:{current_event_index-necessary_events}")
                break
            if current_event_index>necessary_events and model==2:
                log_open = True
                line_index = line_index_must


        if not match and line_index == len(log_lines) - 1 and check_number+1 <= necessary_events:

            do_log.info(f"未检索到匹配的日志结果,事件名字为{event_name}")
            do_log.info(f"验证失败。期望的必要事件数量为{necessary_events},匹配到的事件数量为{current_event_index}")
            break
        if not match and line_index == len(log_lines) - 1 and current_event_index==necessary_events and check_number+1<=len(events_list) and model==1:
            do_log.info(f"第{check_number+1}次未检索到匹配的日志结果,事件名为{event_name}")
            do_log.info(f"非必要事件验证失败。需要验证的非必要事件名为{event_pattern}")
            check_number += 1
            log_open = True
            line_index=line_index_must
        if not match and line_index == len(log_lines) - 1 and model==2:
            do_log.info(f"第{check_number+1}次未检索到匹配的日志结果,事件名为{event_name}")
            do_log.info(f"必要非顺序事件验证失败。必要事件正确个数为{current_event_index},必要非顺序事件正确个数为{current_event_index-necessary_events},需要通过{len(events_list)-necessary_events}个")
            break
        if not match and line_index == len(log_lines) - 1 and check_number+1>len(events_list) :
            do_log.info(f"第{check_number+1}次未检索到匹配的日志结果")
            do_log.info(f"非必要事件验证失败。需要验证的非必要事件名为{event_pattern},事件名为{event_name}")
            break
        line_index+=1

    if model==1:
        assert current_event_index==necessary_events+1 or current_event_index==len(events_list)
    if model==2:
        assert  current_event_index == len(events_list)

sequential_events_log(events_sequential_list,"air.log",necessary_events=4,model=2)